package com.masterclient.app.modules.order;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselib.utils.sp.SPUtils;
import com.eoner.baselibrary.base.FragmentHostActivity;
import com.eoner.baselibrary.bean.order.OrderListBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.widget.CommonTabLayout;
import com.eoner.common.config.Config;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.modules.bridge.BridgeActivity;
import com.masterclient.app.modules.order.adapter.SaleOrderViewPagerAdapter;
import com.masterclient.app.modules.purchase.widget.GuideSaleOrderDialog;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @auther hjian
 * @date 2020/9/18.
 */
@Route(path = AroutePath.DEALER_SALE_ORDERS_ACTIVITY)
public class DealerSaleOrdersActivity extends BaseActivity {

    @BindView(R.id.sale_orders_title)
    TextView saleOrdersTitle;
    @BindView(R.id.sale_orders_bg)
    ImageView saleOrdersBg;
    @BindView(R.id.sale_orders_desc)
    TextView saleOrdersDesc;
    @BindView(R.id.sale_orders_auto_title)
    TextView saleOrdersAutoTitle;
    @BindView(R.id.sale_orders_auto_total)
    TextView saleOrdersAutoTotal;
    @BindView(R.id.sale_orders_auto_unit)
    TextView saleOrdersAutoUnit;
    @BindView(R.id.sale_orders_auto_desc)
    TextView saleOrdersAutoDesc;
    @BindView(R.id.sale_orders_auto_right)
    LinearLayout saleOrdersAutoRight;
    @BindView(R.id.sale_orders_auto_layout)
    LinearLayout saleOrdersAutoLayout;
    @BindView(R.id.sale_orders_back)
    RelativeLayout saleOrdersBack;
    @BindView(R.id.sale_orders_after_sale)
    TextView saleOrdersAfterSale;
    @BindView(R.id.sale_orders_bar)
    Toolbar saleOrdersBar;
    @BindView(R.id.sale_orders_head)
    CollapsingToolbarLayout saleOrdersHead;
    @BindView(R.id.sale_orders_tab)
    CommonTabLayout saleOrdersTab;
    @BindView(R.id.sale_orders_coordinator)
    CoordinatorLayout saleOrdersCoordinator;
    @BindView(R.id.sale_orders_appbar)
    AppBarLayout saleOrdersAppbar;
    @BindView(R.id.sale_orders_pager)
    ViewPager saleOrdersPager;
    @BindView(R.id.sale_orders_help)
    ImageView saleOrdersHelp;

    @Autowired(name = "auto_type")
    int autoType = 0;
    @Autowired(name = "position")
    int position = 0;

    private List<String> tabs;
    private List<Fragment> fragments;
    private SaleOrderViewPagerAdapter viewPagerAdapter;

    private String helpUrl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dealer_sale_orders);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        ImmersionBar.with(this).init();

        int statusHeight = ImmersionBar.getStatusBarHeight(this);
        saleOrdersBar.setPadding(0,statusHeight,0,0);
        if (autoType == 0){
            saleOrdersTitle.setText("销售订单");
            saleOrdersAfterSale.setVisibility(View.VISIBLE);
            saleOrdersBg.getLayoutParams().height = ScreenUtils.dp2px(240)+statusHeight;
            saleOrdersBg.setImageResource(R.mipmap.sale_order_head_bg);
            saleOrdersAutoLayout.setVisibility(View.VISIBLE);
            saleOrdersHelp.setVisibility(View.VISIBLE);
        }else {
            saleOrdersTitle.setText("自动代发单");
            saleOrdersAfterSale.setVisibility(View.GONE);
            saleOrdersBg.getLayoutParams().height = ScreenUtils.dp2px(208)+statusHeight;
            saleOrdersBg.setImageResource(R.mipmap.sale_order_head_bg_auto);
            saleOrdersAutoLayout.setVisibility(View.GONE);
            saleOrdersHelp.setVisibility(View.GONE);
        }


        bntBg(saleOrdersAfterSale,0.5f, Color.parseColor("#80FFFFFF"),12,Color.parseColor("#40FFFFFF"));
        bntBg(saleOrdersDesc,0, Color.parseColor("#F7FCFF"),14,Color.parseColor("#F7FCFF"));
        bntBg(saleOrdersAutoRight,0.5f, Color.parseColor("#2611A6FE"),10,Color.parseColor("#EBF9FF"));

        tabs = Arrays.asList(getResources().getStringArray(autoType==0?R.array.purchase_order_tabs_name:R.array.sale_order_auto_tabs_name));
        fragments = new ArrayList<>();

        for (int i = 0; i < tabs.size(); i++) {
            saleOrdersTab.addTab(tabs.get(i), i == 0);
            fragments.add(OrderListFragment.getInstance(i==0?"":getResources().getStringArray(autoType == 1?R.array.purchase_order_auto_tabs_code:R.array.purchase_order_tabs_code)[i],"","sale",String.valueOf(autoType)));
        }

        viewPagerAdapter = new SaleOrderViewPagerAdapter
                (getSupportFragmentManager(), fragments, tabs);
        saleOrdersPager.setAdapter(viewPagerAdapter);
        saleOrdersPager.addOnPageChangeListener(new TabLayout.
                TabLayoutOnPageChangeListener(saleOrdersTab.getTabLayout()));
        saleOrdersTab.setupWithViewPager(saleOrdersPager);
        saleOrdersPager.setOffscreenPageLimit(1);

        saleOrdersPager.setCurrentItem(position);

//        IntentFilter filter = new IntentFilter();
//        filter.addAction(action);
//        registerReceiver(mBoradcastReceiver, filter);


    }

    public void addAutoNum(){
        saleOrdersAutoTotal.setText(String.valueOf(Long.parseLong(saleOrdersAutoTotal.getText().toString())+1));
    }

    public void setDesc(String desc, OrderListBean.DataBean.AutoOrderInfoBean auto_order_info, String help_url){

        helpUrl = help_url;
        if (TextUtils.isEmpty(desc)){
            saleOrdersDesc.setVisibility(View.GONE);
        }else {
            saleOrdersDesc.setText(desc.replace("\\n","\n"));
            saleOrdersDesc.setVisibility(View.VISIBLE);
        }
        if (auto_order_info != null) {
            saleOrdersAutoTitle.setText(auto_order_info.getLeft_title());
            saleOrdersAutoTotal.setText(auto_order_info.getAuto_order_num());
            saleOrdersAutoUnit.setText(auto_order_info.getLeft_sub_title());
            saleOrdersAutoDesc.setText(auto_order_info.getRight_title());
        }

        saleOrdersDesc.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
            if (autoType == 0 && SPUtils.getInstance().getBoolean(Config.IS_FIRST_SALE_ORDER,true)){
                new GuideSaleOrderDialog(this, saleOrdersDesc.getHeight()+ScreenUtils.dp2px(56)).show();
                SPUtils.getInstance().put(Config.IS_FIRST_SALE_ORDER, false);
            }
        });
    }

    @OnClick({R.id.sale_orders_back, R.id.sale_orders_auto_layout, R.id.sale_orders_after_sale, R.id.sale_orders_help})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sale_orders_help:
                startActivitry(BridgeActivity.class,new String[][]{{"url",helpUrl}});
                break;
            case R.id.sale_orders_back:
                finish();
                break;
            case R.id.sale_orders_auto_layout:
                ARouter.getInstance().build(AroutePath.DEALER_SALE_ORDERS_ACTIVITY).withInt("auto_type",1).navigation();
                break;
            case R.id.sale_orders_after_sale:
                    Fragment fragment = (Fragment) ARouter.getInstance().build(AroutePath.AFTER_SALE_SERVE_FRAGMENT)
                            .navigation();
                    FragmentHostActivity.openFragment(mContext, fragment);
                break;
        }
    }


    //内容描述：0,#F7FCFF,14,#F7FCFF
    //处理售后: 0.5f,#80FFFFFF,12,#40FFFFFF
    //自动代发：0.5f,#2611A6FE,10,#EBF9FF
    private void bntBg(View view, float strokeWidth, @ColorInt int strokeColor, float radius, @ColorInt int bgColor) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        if (strokeWidth > 0) {
            drawable.setStroke(ScreenUtils.dp2px(strokeWidth), strokeColor);
        }
        drawable.setCornerRadius(ScreenUtils.dp2px(radius));
        drawable.setColor(bgColor);
        view.setBackground(drawable);
    }

    public boolean isDestroy = false;
    @Override
    protected void onDestroy() {
        super.onDestroy();
        isDestroy = true;
    }
}
